package newPan3d.content.npc
{
	import _Pan3D.display3D.Display3D;
	import _Pan3D.display3D.Display3DContainer;
	import _Pan3D.display3D.Display3DRenderContainer;
	import _Pan3D.display3D.analysis.ObjAnalysis;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.program.shaders.Md5Shader;
	import _Pan3D.role.Hero;
	import _Pan3D.role.Npc;
	import _Pan3D.role.Role;
	import _Pan3D.role.RoleVo;
	
	import _me.Scene_data;
	
	import com.Astart.Arrange;
	
	import flash.display3D.Context3D;
	import flash.display3D.Program3D;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.drm.VoucherAccessInfo;
	import flash.utils.Timer;

	public class NpcLevel
	{
		private var npcContainer:Display3DRenderContainer;
	    public  static var  npcItem:Vector.<Npc>;
		private var configSource:Array = ["role2.xml","role3.xml"];
		private var configList:Vector.<RoleVo> = new Vector.<RoleVo>;
		private var flag:int;
		
		private var timer:Timer;
		public function NpcLevel()
		{
			loadRoleConfig();
		}
		private function addNpcs():void
		{
			npcItem=new Vector.<Npc>;
			var context3D : Context3D = Scene_data.context3D;
			npcContainer=new Display3DRenderContainer;
			npcContainer.compareFun = compare;
			var md5Program:Program3D = Program3DManager.getInstance().getProgram(Md5Shader.MD5SHADER);
			npcContainer.context3D = context3D;
			npcContainer.program = md5Program;
			
			var info:RoleVo;
			for(var i:int;i<100;i++){
				info = configList[int(configList.length*Math.random())];
				//info = configList[1];
				var npc:Npc = new Npc(context3D,info);
				npc.id=i;
				npc.scale=info.scale;
				npc.x=1000*Math.random();
				npc.y=0
				npc.z=1000*Math.random();
				npc.rotationY=0;
				npcContainer.addChild(npc);
				npc.setProgram3D(md5Program);
				npc.frameRate = 0;
				
				var rec:Rectangle = new Rectangle(0,0,200,200);
				npc.moveRec = rec;
				npc.action_move();
				
				npcItem.push(npc);
			}
			
		}
		private function refreshNpc(event:Event=null):void{
			npcContainer.refreshRenderlist(Scene_data.self3DMove);
			for(var i:int;i<npcContainer.renderList.length;i++){
				if(npcContainer.renderList[i].info.type == "b"){
					Npc(npcContainer.renderList[i]).magicFire(Scene_data.self3DMove);
					Scene_data.huoqiuLevel.setHuoqiuLoadObj(Npc(npcContainer.renderList[i]));
					break;
				}
				
			}
		}
		public function upData():void
		{
			if(npcContainer)
				npcContainer.update();
		}
		
		
		private function loadRoleConfig():void{
			var urlloader:URLLoader = new URLLoader();
			urlloader.addEventListener(Event.COMPLETE,onCom);
			urlloader.load(new URLRequest("../_File/nijia2/role/" + configSource[flag]));
		}
		private function onCom(event:Event):void{
			var xml:XML = XML(event.target.data);
			var name:String = String(xml.name);
			var scale:Number = Number(xml.scale);
			var type:String = String(xml.type)
			var equipment:Object = new Object
			for each (var prop:XML in xml.equipment.prop)
			{
				equipment[String(prop.@place)] = String(prop.@url);
			}
			var action:Object = new Object;
			for each (prop in xml.action.prop)
			{
				var obj:Object = new Object;
				obj.url = String(prop.@url);
				obj.zyklus = Boolean(int(prop.@zyklus))
				action[String(prop.@name)] = obj;
			}
			
			var info:RoleVo = new RoleVo;
			info.name = name;
			info.equipment = equipment;
			info.action = action;
			info.scale = scale;
			info.type = type;
			
			configList.push(info);
			
			//npcContainer.compareFun = compare;
			/*
			addNpcs(info);
			
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER,refreshNpc);
			refreshNpc();
			timer.start();
			*/
			flag++;
			if(flag == configSource.length){
				addNpcs();
				
				timer = new Timer(1000);
				timer.addEventListener(TimerEvent.TIMER,refreshNpc);
				refreshNpc();
				timer.start();
			}else{
				loadRoleConfig();
			}
		}
		private function compare(target:Display3D,child:Display3D):Boolean{
			var xx:Number = Math.abs(target.x - child.x);
			var yy:Number = Math.abs(target.z - child.z);
			if(xx < 100 && yy < 100){
				return true;
			}else{
				return false;
			}
		}
	}
}